home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
boe.pres.k12.wv.us
/
boe.pres.k12.wv.us.zip
/
boe.pres.k12.wv.us
/
Utilities
/
Xerox Workcentre 5335
/
Windows Scan
/
64-bit_x64
/
Russian
/
cpsimage.cab
/
data
/
sys
/
unit.elf
< prev
next >
Wrap
Text File
|
2009-04-23
|
5KB
|
171 lines
/*
** $Id: unit.elf,v 1.6 2008/10/03 20:20:53 campanel Exp $
*/
#load "sys/lang.elf";
/******************************************************************************/
/*
** This class provides unit testing to the language.
*/
/* @assertTrue Asserts a value is true. */
/* @assertFalse Asserts a value is true. */
/* @assertNull Asserts a value is null. */
/* @assertNotNull Asserts a value is not null. */
/* @assertEqual Asserts two values are equal. */
/* @assertNotEqual Asserts two values are not equal. */
/******************************************************************************/
private
CLASS UNITTEST {
INTEGER FailedAsserts = 0;
INTEGER Asserts = 0;
METHOD assertTrue( BOOLEAN cond, STRING msg )
{
this.Asserts++;
if( !cond ) {
this.fail( cond:"not TRUE", msg:msg );
}
}
METHOD assertFalse( BOOLEAN cond, STRING msg )
{
this.Asserts++;
if( cond ) {
this.fail( cond:"not FALSE", msg:msg );
}
}
METHOD assertNull( OBJECT obj, STRING msg )
{
this.Asserts++;
if( obj ) {
this.fail( cond:"not NULL", msg:msg );
}
}
METHOD assertNotNull( OBJECT obj, STRING msg )
{
this.Asserts++;
if( !obj ) {
this.fail( cond:"is NULL", msg:msg );
}
}
METHOD assertEqual( OBJECT exp, OBJECT act, STRING msg )
{
this.Asserts++;
if( (!act && !exp) || (exp && (act == exp)) ) {
return;
}
this.fail( cond:"expected " + exp + " but got " + act, msg:msg );
}
METHOD assertNotEqual( OBJECT exp, OBJECT act, STRING msg )
{
this.Asserts++;
if( (!act && !exp) || (exp && (act == exp)) ) {
this.fail( cond:"is EQUAL", msg:msg );
}
}
METHOD fail( STRING cond, STRING msg )
{
CLASSREF cr = new( CLASSREF, handle:this );
STRING fmt = "Test failed in " + cr.getName() + ", " + cond;
if( msg ) {
fmt = fmt + ": " + msg;
}
print fmt;
DEBUGGER.printStackTrace();
DEBUGGER.log( msg:fmt );
print "";
this.FailedAsserts++;
}
}
/******************************************************************************/
/*
** This executes all known unit tests.
*/
/******************************************************************************/
private
PROCEDURE ExecUnitTests( BOOLEAN verbose, BOOLEAN noRun )
{
INTEGER tests = 0;
INTEGER asserts = 0;
INTEGER failed = 0;
CLASSREF ut = REFLECTION.findClass( name:"UNITTEST" );
CLASSREF cr;
OBJECT obj;
LIST methods;
BOOLEAN hassetup, hasteardown;
/* Get all classes */
LIST classes = REFLECTION.listClasses();
foreach( classes : c ) {
cr = REFLECTION.findClass( name:c );
/* Does it extend UNITTEST */
if( ut.isAssignableFrom( cls:cr ) ) {
methods = cr.listMethods();
hassetup = cr.hasMethod( name:"setUp" );
hasteardown = cr.hasMethod( name:"tearDown" );
obj = cr.newInstance();
foreach( methods : m ) {
if( m.indexOf( match:"test" ) == 0 ) {
if ( hassetup && verbose && noRun )
print "Executing " + c + ".setUp";
else if ( hassetup && verbose ) {
print "Executing " + c + ".setUp";
cr.invokeMethod( obj:obj, name:"setUp" );
}
else if ( hassetup )
cr.invokeMethod( obj:obj, name:"setUp" );
if ( verbose && noRun )
print "Executing " + c + "." + m;
else if ( verbose ) {
print "Executing " + c + "." + m;
cr.invokeMethod( obj:obj, name:m );
}
else
cr.invokeMethod( obj:obj, name:m );
if ( hasteardown && verbose && noRun )
print "Executing " + c + ".tearDown";
else if ( hasteardown && verbose ) {
print "Executing " + c + ".tearDown";
cr.invokeMethod( obj:obj, name:"tearDown" );
}
else if( hasteardown )
cr.invokeMethod( obj:obj, name:"tearDown" );
tests++;
}
}
asserts = asserts + cr.getField( obj:obj, name:"Asserts" );
failed = failed + cr.getField( obj:obj, name:"FailedAsserts" );
}
}
print "TOTAL Assertions " + asserts + ", Tests " + tests;
if( failed != 0 ) {
print "FAILED Assertions " + failed;
SetStatus( op:"Stop", msg:"Unit tests failed" );
}
}